package com.leyu.mall.admin.cms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.common.model.ContentModel;
import com.leyu.mall.domain.cms.CmsChannel;
import com.leyu.mall.domain.cms.CmsContent;
import com.leyu.mall.service.cms.ICmsChannelService;
import com.leyu.mall.service.cms.ICmsContentService;
/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/contents")
public class CmsContentController {
	@Resource(name = "cmsContentServiceImpl")
	private ICmsContentService cmsContentServiceImpl;
	@Autowired
	private ICmsChannelService	cmsChannelService;
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, CmsContent cmsContent) {
		
		try {
			System.out.println("CmsContentController................."+cmsContent);
			cmsContentServiceImpl.save(cmsContent);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../contents";
	}

	@RequestMapping(value = "delete/{contentId}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long contentId) {
		try {
			cmsContentServiceImpl.delete(contentId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../contents";
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long id) {
		try {
			model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
			model.addAttribute("entity", cmsContentServiceImpl.find(id));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editcontent.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request) {
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newContent(Model model) throws Exception {
		
		model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		return "admin/newcontent.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception {
		try {
			List<ContentModel> modelList = new ArrayList();
			List<CmsContent> cmscontentList = cmsContentServiceImpl.getcmsContentList();
			List<CmsChannel> cmschannelList = cmsChannelService.getcmsChannelList();
			for (CmsContent cmscontent : cmscontentList)
			{
				for (CmsChannel cmschannel : cmschannelList)
				{
					if (cmscontent.getChannelID() == cmschannel.getChannelId())
					{
						ContentModel cm = new ContentModel();
						BeanUtils.copyProperties(cmscontent, cm);
						cm.setChannelTitle(cmschannel.getChannelTitle());
						modelList.add(cm);
					}
				}
			}
			model.addAttribute("list", modelList);
			
//		model.addAttribute("list", cmsContentServiceImpl.getcmsContentList());
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return "admin/content.html";
	}
}
